package com.unkow.first.telegram.util;

import com.unkow.first.telegram.bot.TelPollingBotWithMsgHandler;
import com.unkow.first.telegram.handler.TelegramMessageHandler;
import com.unkow.first.telegram.vo.TelBotConfigVO;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.DefaultBotOptions.ProxyType;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage.SendMessageBuilder;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboard;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

@Slf4j
public final class TelBotUtil {

    public TelegramLongPollingBot initBot(TelBotConfigVO telBotConfigVO, TelegramMessageHandler messageHandler) throws TelegramApiException {
        DefaultBotOptions botOptions = new DefaultBotOptions();
        if (telBotConfigVO.getProxyPort() > 0) {
            botOptions.setProxyPort(telBotConfigVO.getProxyPort());
            botOptions.setProxyHost(telBotConfigVO.getProxyHost());
            botOptions.setProxyType(ProxyType.SOCKS5);
        }
        botOptions.setMaxWebhookConnections(10);
        TelegramLongPollingBot bot = new TelPollingBotWithMsgHandler(botOptions, telBotConfigVO, messageHandler);
        botMap.put(telBotConfigVO.getBotToken(), bot);
        return bot;
    }

    public TelegramLongPollingBot getBot(String botToken) {
        return botMap.get(botToken);
    }

    public int sendMessage(String botToken, String baseURL, final String chatId, final String message, String parseMode, ReplyKeyboard replyMarkup) {
        try {
            SendMessageBuilder messageBuilder = SendMessage.builder().chatId(chatId).text(message);
            if (replyMarkup != null) {
                messageBuilder.replyMarkup(replyMarkup);
            }
            if (StringUtils.hasLength(parseMode)) {
                messageBuilder.parseMode(parseMode);
            }
            DefaultBotOptions defaultBotOptions = new DefaultBotOptions();
            if (!ObjectUtils.isEmpty(baseURL)) {
                defaultBotOptions.setBaseUrl(baseURL);
            }
            botMap.get(botToken).execute(messageBuilder.build());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return 500;
        }
        return 200;
    }

    public int sendMessage(String botToken, final String chatId, final String message, String parseMode, ReplyKeyboard replyMarkup) {
        return sendMessage(botToken, null, chatId, message, parseMode, replyMarkup);
    }

    public int sendMessage(String botToken, final String chatId, final String message, String parseMode) {
        return sendMessage(botToken, chatId, message, parseMode, null);
    }

    public int sendMessage(String botToken, final String chatId, final String message) {
        return sendMessage(botToken, chatId, message, "MarkdownV2");
    }

    private final Map<String, TelegramLongPollingBot> botMap = new ConcurrentHashMap<>();
    public final static TelBotUtil handler = new TelBotUtil();

    private TelBotUtil() {

    }
}
